﻿Imports BUS
Imports DTO
Public Class UserControlTraCuuPhieuGui
    Private pgtCrt As New PhieuGoiTienCrt()
    Private Sub CapNhatTieuChuanTraCuu(ByVal bUpdated As Boolean)
        If (bUpdated) Then
            If (txtMaSo.Text <> "") Then
                pgtCrt.MaSo = Integer.Parse(txtMaSo.Text)
            Else
                pgtCrt.MaSo = 0
            End If
            pgtCrt.TenKH = txtTenKH.Text
            If (chkSoTienGoi.Checked) Then
                pgtCrt.SoTienGoiTu = Double.Parse(txtSoTienGoiTu.Text)
                pgtCrt.SoTienGoiDen = Double.Parse(txtSoTienGoiDen.Text)
            Else
                pgtCrt.SoTienGoiTu = 0.0
                pgtCrt.SoTienGoiDen = 0.0
            End If
            If chkNgayGoi.Checked Then
                pgtCrt.NgayGoiTu = dtpNgayGoiTu.Value
                pgtCrt.NgayGoiDen = dtpNgayGoiDen.Value
            End If
            pgtCrt.ChkTenKH = chkKhachHang.Checked
            pgtCrt.ChkSoTienGoi = chkSoTienGoi.Checked
            pgtCrt.ChkNgayGoi = chkNgayGoi.Checked
        End If
    End Sub

    Private Sub btnTraCuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuu.Click
        Dim maso As Integer
        If txtMaSo.Text.Length > 0 And (Integer.TryParse(txtMaSo.Text, maso) = False Or maso <= 0) Then
            MessageBox.Show("Mã sổ không hợp lệ!")
            txtMaSo.Focus()
            Return
        End If
        Dim temp As Double
        Dim temp1 As Double
        If chkSoTienGoi.Checked = True And (Double.TryParse(txtSoTienGoiTu.Text, temp) = False Or temp < 0) Then
            MessageBox.Show("Số tiền gửi từ không hợp lệ!")
            txtSoTienGoiTu.Focus()
            Return
        End If
        If chkSoTienGoi.Checked = True And (Double.TryParse(txtSoTienGoiDen.Text, temp1) = False Or temp1 < 0) Then
            MessageBox.Show("Số tiền gửi đến không hợp lệ!")
            txtSoTienGoiDen.Focus()
            Return
        End If
        If temp > temp1 Then
            MessageBox.Show("Số gửi từ lớn hơn sô tiền gửi đến, không hợp lệ!")
            txtSoTienGoiTu.Focus()
            Return
        End If
        Dim dspgt As New List(Of PhieuGoiTienCrt)()
        Try
            CapNhatTieuChuanTraCuu(True)
            Dim pgtBUS As New PhieuGoiTienBUS()
            dspgt = pgtBUS.TimKiemTheoNhieuTieuChi(pgtCrt)
            If dspgt.Count <= 0 Then
                MessageBox.Show("Không tìm thấy dữ liệu")
                Return
            End If
            Dim frmDS As New frmDanhSachTraCuuPhieuGui()
            frmDS.DSPGT = dspgt
            frmDS.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
     
    End Sub


    Private Sub chkKhachHang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkKhachHang.CheckedChanged
        If chkKhachHang.Checked Then
            txtTenKH.Enabled = True
        Else
            txtTenKH.Enabled = False
        End If
    End Sub

    Private Sub chkSoTienGoi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSoTienGoi.CheckedChanged
        If chkSoTienGoi.Checked Then
            txtSoTienGoiTu.Enabled = True
            txtSoTienGoiDen.Enabled = True
        Else
            txtSoTienGoiTu.Enabled = False
            txtSoTienGoiDen.Enabled = False
        End If
    End Sub

    Private Sub chkNgayGoi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNgayGoi.ClientSizeChanged
        If chkNgayGoi.Checked Then
            dtpNgayGoiTu.Enabled = True
            dtpNgayGoiDen.Enabled = True
        Else
            dtpNgayGoiTu.Enabled = False
            dtpNgayGoiDen.Enabled = False
        End If
    End Sub
End Class
